//
//  DayTicketViewController.m
//  didacar
//
//  Created by imac on 15/12/5.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "DayTicketViewController.h"
#import "CalendarViewController.h"
#import "CalendarDayModel.h"
#import "ResponseDayModel.h"
#import "MainInfoTitleFields.h"
#import "SVProgressHUD.h"
#import "H5BaseViewController.h"
#import "H5Api.h"
#import "DayTicketViewModel.h"
#import "PayTicketViewController.h"

@interface DayTicketViewController ()

// 规则
- (IBAction)ruleBtnAction:(UIButton *)sender;

// 申请班次
- (IBAction)replayBtnAction:(UIButton *)sender;

// 确定购买
- (IBAction)bugBtnAction:(UIButton *)sender;
@property (strong, nonatomic) DayTicketViewModel *dayTicketViewModel;
@property (strong, nonatomic) IBOutlet UILabel *priceLabel;
@property (strong, nonatomic) IBOutlet UILabel *dayCountLabel;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *line_height;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *ruleLine_height;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *replayLine_height;

@property (strong, nonatomic) IBOutlet UIView *bottomView;
@property (strong, nonatomic) CalendarViewController *dayTicket;

@property (strong, nonatomic) NSMutableArray *selectedDayTickets;


@end

@implementation DayTicketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.selectedDayTickets = [[NSMutableArray alloc ] init];
    self.dayTicketViewModel = [[DayTicketViewModel alloc ] init];
    
    [self setLeftBtnItem];
    
    self.line_height.constant = 0.45;
    self.ruleLine_height.constant = 0.45;
    self.replayLine_height.constant = 0.45;
    

    [self updateBottomData];
    
   
    [self.view bringSubviewToFront:self.bottomView];
    
    self.dayTicketViewModel.LineId = @(self.entity.LineId).stringValue;
    self.dayTicketViewModel.BCID   = self.entity.BCID;
    self.dayTicketViewModel.StartStationId = self.entity.StartStationId;
    self.dayTicketViewModel.EndStationId = self.entity.EndStationId;
    self.dayTicketViewModel.IsSale  = self.entity.IsSale;
    WEAKSELF
    [self.dayTicketViewModel loadDayTicketData:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [weakSelf pusnDayTicket];
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
    }];
}

-(void)pusnDayTicket{
    self.dayTicket = [[CalendarViewController alloc ] init];
    self.dayTicket.selectedEnable = YES;
    [self.dayTicket responseDayModels:self.dayTicketViewModel.dayTicketPrices];
    self.dayTicket.view.frame = CGRectMake(0, 0, [UIScreen width], [UIScreen height] -164);
    [self.dayTicket ajustFrame:CGRectMake(0, 0,  [UIScreen width], [UIScreen height] -164)];
    
    WEAKSELF
    self.dayTicket.calendarblock = ^void(CalendarDayModel *model){
        
        // 如果是选中
        if (model.style == CellDayTypeClick) {
            [weakSelf.selectedDayTickets addObject:model];
        }else{
            [weakSelf.selectedDayTickets removeObject:model];
        }
        
        [weakSelf updateBottomData];
        
    };
    
    [self addChildViewController:self.dayTicket];
    [self.view addSubview:self.dayTicket.view];
}

-(void)updateBottomData{
    
   
    self.priceLabel.text = @([self getTotalPrice]).stringValue;
    self.dayCountLabel.attributedText = [self getDayCountString];
}

-(CGFloat)getTotalPrice{
    CGFloat totalPrice = 0.0;
    for (CalendarDayModel *model in self.selectedDayTickets) {
        totalPrice = totalPrice + [model.price  floatValue];
    }
    return totalPrice;
}

-(NSAttributedString *)getDayCountString{
    
    NSMutableArray *mutableString = [[NSMutableArray alloc ] init];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:@"共" hexColor:GLOBLE_TEXT_COLOR_HEX fontSize:@"13"]];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:@(self.selectedDayTickets.count).stringValue hexColor:GLOBLE_ORANGE_COLOR_HEX fontSize:@"17"]];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:@"天" hexColor:GLOBLE_TEXT_COLOR_HEX fontSize:@"13"]];
    return [CommoneTools getAttributeString:mutableString];
}


// 规则
- (IBAction)ruleBtnAction:(UIButton *)sender {
    H5BaseViewController *h5Base = [[H5BaseViewController alloc ] init];
    h5Base.webViewUrl = BUYRETURNTICKET;
    [self.navigationController pushViewController:h5Base animated:YES];
}

// 申请班次
- (IBAction)replayBtnAction:(UIButton *)sender {
    H5BaseViewController *h5Base = [[H5BaseViewController alloc ] init];
    h5Base.webViewUrl = ADDCLASSCOUNT;
    [self.navigationController pushViewController:h5Base animated:YES];
}

// 确定购买
- (IBAction)bugBtnAction:(UIButton *)sender {
    
    if (self.selectedDayTickets.count == 0) {
        [SVProgressHUD showErrorWithStatus:@"至少选择一张日票"];
        return;
    }
    
    PayTicketViewController *payTicket = [[UIStoryboard storyboardWithName:@"Ticket" bundle:nil]  instantiateViewControllerWithIdentifier:@"PayTicketViewController"];
    payTicket.payTicketStyle = PayTicket_ThroughBus;
    payTicket.entity         = self.entity;
    payTicket.dayTickets   = self.selectedDayTickets;
    payTicket.payTicketStyle = PayTicket_Day;
    payTicket.totalDay = @(self.selectedDayTickets.count).stringValue;
    payTicket.totalDayPrice = @([self getTotalPrice]).stringValue;
    
    
    [self.navigationController pushViewController:payTicket animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
